﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace GUI
{
    public partial class Thôngtinsách : UserControl
    {
        public Thôngtinsách()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        DataTable mvc =new DataTable();
        DataSet ds = new DataSet();

        private void Thôngtinsách_Load(object sender, EventArgs e)
        {
            ds = ThongtinsachDAO.Loaisach_sach(DataProvider.ketnoi());

            dt = DataProvider.GetDataTable("Loaisach",DataProvider.ketnoi());
            mvc = DataProvider.GetDataTable("Sach",DataProvider.ketnoi());
            LoadTreeview();
        }
        void LoadTreeview(DataRow dr)
        {
            lvwSach.Items.Clear();
            int istt = 0;
            foreach (DataRow drw in dr.GetChildRows("LS_Sach"))
            {
                istt++;
                ListViewItem ls = new ListViewItem();
                ls.Text = istt.ToString();
                ls.SubItems.Add(drw["MaSach"].ToString());
                ls.SubItems.Add(drw["TenSach"].ToString());
                ls.SubItems.Add(drw["SoLuong"].ToString());
                ls.SubItems.Add(drw["MaTG"].ToString());
                ls.SubItems.Add(drw["Maloai"].ToString());
                ls.ImageIndex = 0;
                lvwSach.Items.Add(ls);

            }
        }

        void LoadTreeview()
        {
            foreach (DataRow drls in ds.Tables["LS"].Rows)
            {
                TreeNode td = new TreeNode();
                td.Text = drls["LoaiSach"].ToString();
                td.Tag = drls;//luu tru 1 cai ma
                foreach (DataRow drDT in drls.GetChildRows("LS_Sach"))
                {
                    TreeNode trdt = new TreeNode();
                    //trdt=td.Nodes.Add("Ma DT:"+ ""+drDT["MaDT"].ToString());
                    trdt = td.Nodes.Add(drDT["TenSach"].ToString());
                    trdt.Tag = drDT;
                }
                tvwloaisach.Nodes.Add(td);
            }

        }

        private void tvwloaisach_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataRow dr;
            if (tvwloaisach.SelectedNode != null)
                if (tvwloaisach.SelectedNode.Parent == null)//truy xuat nguoc lai thang cha
                {
                    dr = (DataRow)tvwloaisach.SelectedNode.Tag;//kiem tra theo ma Tag
                    LoadTreeview(dr);
                }
                else
                {
                    dr = (DataRow)tvwloaisach.SelectedNode.Tag;
                    hienthi(dr);

                }
        }

        void hienthi(DataRow dr)
        {
            TxtMa.Text = dr["MaSach"].ToString();
            TxtTen.Text = dr["TenSach"].ToString();
            TxtSL.Text = dr["SoLuong"].ToString();
            Txtmatg.Text = dr["MaTG"].ToString();
            TxtMLoai.Text = dr["Maloai"].ToString();

        }

        private void lvwSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwSach.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwSach.SelectedItems[lvwSach.SelectedItems.Count - 1];
                if (item != null)
                {
                    foreach (ListViewItem lvw in lvwSach.SelectedItems)
                    {
                        TxtMa.Text = lvw.SubItems[1].Text;
                        TxtTen.Text = lvw.SubItems[2].Text;
                        TxtSL.Text = lvw.SubItems[2].Text;
                        Txtmatg.Text = lvw.SubItems[4].Text;
                        TxtMLoai.Text = lvw.SubItems[5].Text;

                    }
                }
            }
        }



    }
}
